var express = require('express');
var router = express.Router();

const { uploadFiles } = require('../utils/handleFiles');
const { addClaims, getClaims, editClaims, deleteClaims, getClaimApply, noClaimsApply, getFinishClaimApply, okClaimsApply } = require('../service/claimsService')

//图片上传
router.post('/claimsImages', function (req, res, next) {
    const headPortrait = uploadFiles({
        path: './public/temporary',     //temporary
        key: 'file',
        size: 7000
    })
    headPortrait(req, res, (err) => {
        if (err) {
            console.log('上传失败', err);
        } else {
            console.log('上传成功');
            res.send({
                message: '图片上传成功',
                status: 1,
                data: req.files[0].filename
            })
        }
    })
});

//add claims
router.post('/addClaims', async (req, res, next) => {
    const { applyDate, troubleDate, claimsImages, order } = req.body;
    res.send(await addClaims({ applyDate, troubleDate, claimsImages, order }))
})

//get claims
router.get('/getClaims', async (req, res, next) => {
    res.send(await getClaims())
})

//edit claims
router.post('/editClaims', async (req, res, next) => {
    const { _id, applyDate, troubleDate, claimsImages, order } = req.body;
    res.send(await editClaims(_id, { applyDate, troubleDate, claimsImages, order }))
})

//delete claims
router.post('/deleteClaims', async (req, res, next) => {
    const { _id } = req.body;
    res.send(await deleteClaims({ _id }))
})
//管理人员查看待处理理赔
router.get('/getClaimApply', async (req, res, next) => {
    // console.log(await getClaimApply(req.query));
    res.send(await getClaimApply(req.query))
})
//查看已审核的理赔
router.get('/getFinishClaimApply', async (req, res, next) => {
    res.send(await getFinishClaimApply(req.query))
})
//管理人员处理，同意理赔
router.post('/okClaimsApply', async (req, res, next) => {
    res.send(await okClaimsApply(req.body))
})
//管理人员处理，不同意理赔
router.post('/noClaimsApply', async (req, res, next) => {
    res.send(await noClaimsApply(req.body))
})
module.exports = router;
